Perl - Copy files under 5 mins old

chris (2003-06-30 23:56:09)
3825 views
0 replies
Somebody asked me recently to knock up a PERL script which would copy all files in one directory, which were less than 5 mins old, over to another directory. An odd request, but the result involved a nifty use of 'stat' (thanks to Jodrell for the pointer).

#!/usr/bin/perl
use strict;
use File::Copy;
 
my $age;
my $destination="/home/chris/tmp/";
my $sourcedir="/home/chris/bin/";
 
my @files=`cd $sourcedir && ls`;
 
# iterate and copy each one
foreach (@files){
        chomp;
        # get the age in seconds
        $age = (time() - (stat($sourcedir.$_))[9]);
        print "\n$sourcedir.$_ is is $age seconds old\n";
        if($age<300){
                copy("$sourcedir$_","$destination$_");
        }
}
comment